package com.lineage.server.timecontroller.event.ranking;

import com.lineage.DatabaseFactory;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class RankingHeroTimerlv extends TimerTask {
  private static final Log _log = LogFactory.getLog(RankingHeroTimerlv.class);
  
  private ScheduledFuture<?> _timer;
  
  private static boolean _load = false;
  
  private static String[] _userNameAll;
  
  private static String[] _userNameC;
  
  private static String[] _userNameK;
  
  private static String[] _userNameE;
  
  private static String[] _userNameW;
  
  private static String[] _userNameD;
  
  private static String[] _userNameG;
  
  private static String[] _userNameI;
  
  private static String[] _userNameWarrior;
  
  private static Map<Integer, String> _top10 = new HashMap<>();
  
  private static Map<Integer, String> _top3C = new HashMap<>();
  
  private static Map<Integer, String> _top3K = new HashMap<>();
  
  private static Map<Integer, String> _top3E = new HashMap<>();
  
  private static Map<Integer, String> _top3W = new HashMap<>();
  
  private static Map<Integer, String> _top3D = new HashMap<>();
  
  private static Map<Integer, String> _top3G = new HashMap<>();
  
  private static Map<Integer, String> _top3I = new HashMap<>();
  
  private static Map<Integer, String> _top3Warrior = new HashMap<>();
  
  public void start() {
    restart();
    int timeMillis = 600000;
    this._timer = GeneralThreadPool.get().scheduleAtFixedRate(this, 600000L, 600000L);
  }
  
  public static Map<Integer, String> get_top10() {
    return _top10;
  }
  
  public static Map<Integer, String> get_top3C() {
    return _top3C;
  }
  
  public static Map<Integer, String> get_top3K() {
    return _top3K;
  }
  
  public static Map<Integer, String> get_top3E() {
    return _top3E;
  }
  
  public static Map<Integer, String> get_top3W() {
    return _top3W;
  }
  
  public static Map<Integer, String> get_top3D() {
    return _top3D;
  }
  
  public static Map<Integer, String> get_top3G() {
    return _top3G;
  }
  
  public static Map<Integer, String> get_top3I() {
    return _top3I;
  }
  
  public static Map<Integer, String> get_top3Warrior() {
    return _top3Warrior;
  }
  
  public static String[] userNameAll() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameAll[i].split(",");
      newUserName[i] = set[0];
      if (CharacterTable.doesCharNameExist(newUserName[i]))
        _top10.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameC() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameC[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3C.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameK() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameK[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3K.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameE() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameE[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3E.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameW() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameW[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3W.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameD() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameD[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3D.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameG() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameG[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3G.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameI() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameI[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3I.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public static String[] userNameWarrior() {
    if (!_load)
      load(); 
    String[] newUserName = new String[10];
    int i = 0;
    while (i < 10) {
      String[] set = _userNameWarrior[i].split(",");
      newUserName[i] = set[0];
      if (i < 3 && CharacterTable.doesCharNameExist(newUserName[i]))
        _top3Warrior.put(Integer.valueOf(i), newUserName[i]); 
      i++;
    } 
    return newUserName;
  }
  
  public void run() {
    try {
      load();
    } catch (Exception e) {
      _log.error("英雄风云榜时间轴异常重启", e);
      GeneralThreadPool.get().cancel(this._timer, false);
      RankingHeroTimerlv heroTimer = new RankingHeroTimerlv();
      heroTimer.start();
    } 
  }
  
  public static void load() {
    try {
      _load = true;
      restart();
      Connection con = null;
      PreparedStatement pstm = null;
      ResultSet rs = null;
      try {
        con = DatabaseFactory.get().getConnection();
        pstm = con.prepareStatement(
            "SELECT * FROM `characters` WHERE `logpcpower` >= 0 and `level` > 0 and `AccessLevel` <= 0 ORDER BY `logpcpower` DESC,`Exp` DESC");
        rs = pstm.executeQuery();
        while (rs.next()) {
          String char_name = rs.getString("char_name");
          int level = rs.getInt("level");
          int type = rs.getInt("Type");
          if (type == 0) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameC[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameC[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } else if (type == 1) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameK[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameK[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } else if (type == 2) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameE[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameE[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } else if (type == 3) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameW[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameW[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } else if (type == 4) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameD[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameD[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } else if (type == 5) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameG[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameG[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } else if (type == 6) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameI[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameI[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } else if (type == 7) {
            int j = 0;
            int k = 10;
            while (j < 10) {
              if (_userNameWarrior[j].equals(" ")) {
                StringBuffer sbr = (new StringBuffer()).append(char_name);
                sbr.append(" :Lv.").append(level).append("");
                _userNameWarrior[j] = sbr.toString();
                break;
              } 
              j++;
            } 
          } 
          int i = 0;
          int n = 10;
          while (i < 10) {
            if (_userNameAll[i].equals(" ")) {
              StringBuffer sbr = (new StringBuffer()).append(char_name);
              sbr.append(" :Lv.").append(level).append("");
              _userNameAll[i] = sbr.toString();
              break;
            } 
            i++;
          } 
          Thread.sleep(1L);
        } 
      } catch (SQLException e) {
        _log.error(e.getLocalizedMessage(), e);
      } finally {
        SQLUtil.close(rs);
        SQLUtil.close(pstm);
        SQLUtil.close(con);
      } 
    } catch (Exception e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } 
  }
  
  private static void restart() {
    _userNameAll = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameC = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameK = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameE = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameW = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameD = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameG = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameI = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
    _userNameWarrior = new String[] { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " };
  }
}
